package com.dimanche.module_course.ui.main;

import android.app.Application;
import android.text.Html;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;

import com.dimanche.library_base.utils.SystemTTS;
import com.dimanche.library_db.entity.WordEntity;
import com.dimanche.module_course.data.CourseRepository;
import com.dimanche.module_course.data.source.Course100Source;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

/**
 * author : Dimanche
 * date   : 2021/3/26 17:35
 * desc   :
 * version: 1.0
 */
public class MainViewModel extends BaseViewModel<CourseRepository> {
    private static final String TAG = "MainViewModel";

    public int position = 0;
    public ObservableField<Boolean> lastVisible = new ObservableField<>();
    public ObservableField<Boolean> nextVisible = new ObservableField<>();
    public WordEntity wordEntityOb = new WordEntity();
    public SingleLiveEvent<LiveData<List<WordEntity>>> datas = new SingleLiveEvent<>();


    public MainViewModel(@NonNull Application application, CourseRepository model) {
        super(application, model);
        lastVisible.set(true);
        nextVisible.set(true);
    }

    /**
     * 获取第一个汉字
     */
    public void getFirstWord() {
        lastVisible.set(false);
        if (Course100Source.WORD.length > 0) {
            datas.setValue(getWord(0));
        } else {
            nextVisible.set(false);
        }

    }

    /**
     * 下一个汉字
     */
    public BindingCommand next = new BindingCommand(() -> {

        if (position < Course100Source.WORD.length - 1) {
            SystemTTS.getInstance().stopSpeak();
            position++;
            datas.setValue(getWord(position));
            if (position > 0) {
                lastVisible.set(true);
            }
            if (position == Course100Source.WORD.length - 1) {
                nextVisible.set(false);
            }
        }

    });

    /**
     * 上一个汉字
     */
    public BindingCommand last = new BindingCommand(() -> {

        SystemTTS.getInstance().stopSpeak();
        if (position > 0) {
            position--;
            datas.setValue(getWord(position));
            if (position < Course100Source.WORD.length - 1) {
                nextVisible.set(true);
            }
            if (position == 0) {
                lastVisible.set(false);
            }
        }

    });

    /**
     * 获取汉字
     */
    public LiveData<List<WordEntity>> getWord(int position) {
        return model.getDirRepository().queryByWord(model.getCourse100Source().getWord(position));
    }


    /**
     * 播放汉字语音
     */
    public BindingCommand playWordTTs = new BindingCommand(() -> {
        Log.i(TAG, ": playTTs");
        SystemTTS.getInstance().playText(wordEntityOb.word);
    });

    /**
     * 播放释义语音
     */
    public BindingCommand playExplanationTTs = new BindingCommand(() -> {
        SystemTTS.getInstance().playText(wordEntityOb.explanation);
    });


}
